package cn.edu.hpu.action;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hpu.pojo.Contacts;
import cn.edu.hpu.pojo.Im;
import cn.edu.hpu.pojo.Mail;
import cn.edu.hpu.pojo.Message;
import cn.edu.hpu.pojo.Tel;
import cn.edu.hpu.pojo.User;
import cn.edu.hpu.pojo.Usergroup;
import cn.edu.hpu.pojo.Website;
import cn.edu.hpu.service.ContactsService;
import cn.edu.hpu.service.ImService;
import cn.edu.hpu.service.MailService;
import cn.edu.hpu.service.MessageService;
import cn.edu.hpu.service.TelService;
import cn.edu.hpu.service.UserService;
import cn.edu.hpu.service.UsergroupService;
import cn.edu.hpu.service.WebsiteService;
import cn.edu.hpu.util.Pager;
import cn.edu.hpu.util.TimeUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class ContactsAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private ContactsService contactsService;
	private Contacts contacts;
	private List<Contacts> clist;
	private UserService userService;
	private List<User> ulist;
	private UsergroupService usergroupService;
	private List<Usergroup> uglist;
	private ImService imService;
	private MailService mailService;
	private TelService telService;
	private WebsiteService websiteService;
	private String id;
	private String uid;
	private String g;
	private String u;
	private MessageService messageService;
	private Contacts c;
	private String cid;
	private String name;
	private Pager<Contacts> pager;
	private int p;
	private User user;
	private Usergroup group;
	private String companyname;
	private String weibo;
	private String weibotype;
	private String content;
	private String address;
	private String view;
	@Override
	public String execute() throws Exception {
		int num=1;
		if (p >=1) {
			num=p;
		}else{
			p = 1;
			num=1;
		}
		pager = contactsService.findByPager(num);
		clist = pager.getResult();
		uglist = usergroupService.findAllUsergroups();
		ulist = userService.findAll();
		return SUCCESS;
	}
	public String findAllContacts(){
		clist = contactsService.findByNameOrCompanyname(name);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("name", name);
		return SUCCESS;
	}
	
	public String savecontacts(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] tels = request.getParameterValues("tel");
		String[] types= request.getParameterValues("type");
		Set<Tel> telSet = new HashSet<Tel>();
		Set<Im> imSet = new HashSet<Im>();
		Set<Mail> mailSet = new HashSet<Mail>();
		Set<Website> websiteSet = new HashSet<Website>();
		for (int i = 0; i < tels.length; i++) {
			Tel t = new Tel();
			t.setCreatetime(TimeUtil.getTime());
			t.setTel(tels[i]);
			t.setContacts(contacts);
			t.setType(types[i]);
			telService.save(t);
			telSet.add(t);
		}
		String[] ims = request.getParameterValues("im");
		for (int i = 0; i < ims.length; i++) {
			Im im = new Im();
			im.setIm(ims[i]);
			im.setContacts(contacts);
			im.setCreatetime(TimeUtil.getTime());
			imService.save(im);
			imSet.add(im);
		}
		String[] mails = request.getParameterValues("mail");
		for (int i = 0; i < mails.length; i++) {
			Mail m = new Mail();
			m.setCreatetime(TimeUtil.getTime());
			m.setMail(mails[i]);
			m.setContacts(contacts);
			mailService.save(m);
			mailSet.add(m);
		}
		String[] websites = request.getParameterValues("website");
		for (int i = 0; i < websites.length; i++) {
			Website w = new Website();
			w.setContacts(contacts);
			w.setCreatetime(TimeUtil.getTime());
			w.setWebsite(websites[i]);
			websiteService.save(w);
			websiteSet.add(w);
		}
		User user = userService.findAUser(uid);
		try {
			Message message = new Message();
			if (contacts.getView().equals("me")) {
				String view = "me"+","+user.getId();
				contacts.setView(view);
				message.setView(view);
			} else if (contacts.getView().equals("use")){
				String view = "use"+","+u;
				contacts.setView(view);
				message.setView(view);
			}else if(contacts.getView().equals("group")){
				String view = "group"+","+g;
				contacts.setView(view);
				message.setView(view);
			}else if(contacts.getView().equals("all")){
				contacts.setView("all");
				message.setView("all");
			}
			contacts.setUser(user);
			contacts.setImSet(imSet);
			contacts.setTelSet(telSet);
			contacts.setMailSet(mailSet);
			contacts.setWebsiteSet(websiteSet);
			contacts.setCreatetime(TimeUtil.getTime());
			contacts.setEnable(true);
			contactsService.save(contacts);
			message.setCreatetime(TimeUtil.getTime());
			message.setContent(contacts.getContent());
			if (contacts.getName()==null) {
				String title =contacts.getCompanyname()+","+"addcontactsnote.action?id="+contacts.getId();
				message.setTitle(title);
			} else {
				String title =contacts.getName()+" "+contacts.getCompanyname()+","+"addcontactsnote.action?id="+contacts.getId();
				message.setTitle(title);
			}
			String type = "联系人"+","+"5FBEEE";
			message.setType(type);
			message.setUser(user);
			message.setCtdid(contacts.getId());
			message.setEnable(true);
			messageService.save(message);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	public String deletecontacts(){
		Contacts c = contactsService.findA(id);
		c.setEnable(false);
		contactsService.update(c);
		Message message = messageService.findA(id);
		message.setEnable(false);
		messageService.update(message);
		return SUCCESS;
	}
	public String edit(){
		c = contactsService.findA(id);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("c", c);
		uglist = usergroupService.findAllUsergroups();
		ulist = userService.findAll();
		clist = contactsService.findAll();
		/*if(c.getView().split(",")[0] == "use"){
			user = userService.findAUser(c.getView().split(",")[1]);
		}else if(c.getView().split(",")[0]=="group"){
			group = usergroupService.findA(c.getView().split(",")[1]);
		}*/
		if (c.getName() != null) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	public String updatecontacts(){
		
		Map<String, Object> map = ActionContext.getContext().getSession();
		User user = (User) map.get("user");
		contacts = contactsService.findA(cid);
		Set<Tel> tels =contacts.getTelSet();
		System.out.println(tels);
		for (Tel t : tels) {
			telService.delete(t.getId());
		}
		Set<Im> ims = contacts.getImSet();
		for (Im im : ims) {
			imService.delete(im.getId());
		}
		Set<Mail> mails =contacts.getMailSet();
		for (Mail mail : mails) {
			mailService.delete(mail.getId());
		}
		Set<Website> websites = contacts.getWebsiteSet();
		for (Website website : websites) {
			websiteService.delete(website.getId());
		}
		contacts.setId(cid);
		System.out.println(contacts.getCompanyname());
		contacts.setCreatetime(TimeUtil.getTime());
		contacts.setUser(user);
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] tels1 = request.getParameterValues("tel");
		String[] types= request.getParameterValues("type");
		Set<Tel> telSet = new HashSet<Tel>();
		Set<Im> imSet = new HashSet<Im>();
		Set<Mail> mailSet = new HashSet<Mail>();
		Set<Website> websiteSet = new HashSet<Website>();
		String[] ims1 = request.getParameterValues("im");
		for (int i = 0; i < tels1.length; i++) {
			Tel t = new Tel();
			t.setCreatetime(TimeUtil.getTime());
			t.setTel(tels1[i]);
			t.setContacts(contacts);
			t.setType(types[i]);
			telService.save(t);
			telSet.add(t);
		}
		for (int i = 0; i < ims1.length; i++) {
			Im im = new Im();
			im.setIm(ims1[i]);
			im.setContacts(contacts);
			im.setCreatetime(TimeUtil.getTime());
			imService.save(im);
			imSet.add(im);
		}
		String[] mails1 = request.getParameterValues("mail");
		for (int i = 0; i < mails1.length; i++) {
			Mail m = new Mail();
			m.setCreatetime(TimeUtil.getTime());
			m.setMail(mails1[i]);
			m.setContacts(contacts);
			mailService.save(m);
			mailSet.add(m);
		}
		String[] websites1 = request.getParameterValues("website");
		for (int i = 0; i < websites1.length; i++) {
			Website w = new Website();
			w.setContacts(contacts);
			w.setCreatetime(TimeUtil.getTime());
			w.setWebsite(websites1[i]);
			websiteService.save(w);
			websiteSet.add(w);
		}
		
		Message message = messageService.findA(cid);
		if (view.equals("me")) {
			String view = "me"+","+user.getId();
			contacts.setView(view);
			message.setView(view);
		} else if (view.equals("use")){
			String view = "use"+","+u;
			contacts.setView(view);
			message.setView(view);
		}else if(view.equals("group")){
			String view = "group"+","+g;
			contacts.setView(view);
			message.setView(view);
		}else if(view.equals("all")){
			contacts.setView("all");
			message.setView("all");
		}
		contacts.setCompanyname(companyname);
		contacts.setAddress(address);
		contacts.setWeibo(weibo);
		contacts.setWeibotype(weibotype);
		contacts.setContent(content);
		contacts.setName(name);
		contacts.setImSet(ims);
		contacts.setTelSet(tels);
		contacts.setMailSet(mails);
		contacts.setWebsiteSet(websites);
		contacts.setUser(user);
		contacts.setEnable(true);
		contactsService.update(contacts);
		message.setCreatetime(TimeUtil.getTime());
		message.setUser(user);
		message.setCtdid(cid);
		message.setContent(content);
		if (contacts.getName()==null) {
			String title =companyname+","+"addcontactsnote.action?id="+cid;
			message.setTitle(title);
		} else {
			String title =name+" "+contacts.getCompanyname()+","+"addcontactsnote.action?id="+cid;
			message.setTitle(title);
		}
		String type = "联系人"+","+"5FBEEE";
		message.setType(type);
		message.setEnable(true);
		messageService.update(message);
		return SUCCESS;
		
	}
	public String export(){
		List<Contacts> contactslist = contactsService.findAll();
		Workbook wb = new HSSFWorkbook();
		Sheet st = wb.createSheet("联系表");
		Row topRow = st.createRow(0);
		
		Cell c1 = topRow.createCell(0);
		c1.setCellValue("name");
		Cell c2 = topRow.createCell(1);
		c2.setCellValue("companyname");
		Cell c3 = topRow.createCell(2);
		c3.setCellValue("weibo");
		Cell c4 = topRow.createCell(3);
		c4.setCellValue("weibotype");
		Cell c5 = topRow.createCell(4);
		c5.setCellValue("content");
		Cell c6 = topRow.createCell(5);
		c6.setCellValue("view");
		Cell c7 = topRow.createCell(6);
		c7.setCellValue("address");
		Cell c8 = topRow.createCell(7);
		c8.setCellValue("createtime");
		
		Cell c10 = topRow.createCell(8);
		c10.setCellValue("tel");
		Cell c11 = topRow.createCell(9);
		c11.setCellValue("im");
		Cell c12 = topRow.createCell(10);
		c12.setCellValue("mail");
		Cell c13 = topRow.createCell(11);
		c13.setCellValue("website");
		for (int i = 1; i < contactslist.size()+1; i++) {
			Row r = st.createRow(i);
			Cell n = r.createCell(0);
			n.setCellValue(contactslist.get(i-1).getName());
			Cell cn = r.createCell(1);
			cn.setCellValue(contactslist.get(i-1).getCompanyname());
			Cell w = r.createCell(2);
			w.setCellValue(contactslist.get(i-1).getWeibo());
			Cell wt = r.createCell(3);
			wt.setCellValue(contactslist.get(i-1).getWeibotype());
			Cell v = r.createCell(4);
			v.setCellValue(contactslist.get(i-1).getContent());
			Cell c = r.createCell(5);
			c.setCellValue(contactslist.get(i-1).getView());
			Cell a = r.createCell(6);
			a.setCellValue(contactslist.get(i-1).getAddress());
			Cell ct = r.createCell(7);
			ct.setCellValue(contactslist.get(i-1).getCreatetime());
			
			Set<Tel> ts = contactslist.get(i-1).getTelSet();
			String str ="";
			for (Tel tel : ts) {
				str +=tel.getTel()+",";
			}
			Cell t = r.createCell(8);
			t.setCellValue(str);
			Set<Im> im = contactslist.get(i-1).getImSet();
			String str1 = "";
			for (Im im2 : im) {
				str1 +=im2.getIm()+","; 
			}
			Cell imm = r.createCell(9);
			imm.setCellValue(str1);
			Set<Mail> ms = contactslist.get(i-1).getMailSet();
			String str2 = "";
			for (Mail mail : ms) {
				str2 +=mail.getMail()+",";
			}
			Cell m = r.createCell(10);
			m.setCellValue(str2);
			Set<Website> ws = contactslist.get(i-1).getWebsiteSet();
			String str3 = "";
			for (Website website : ws) {
				str3 +=website.getWebsite()+",";
			}
			Cell wes = r.createCell(11);
			wes.setCellValue(str3);
		}
		try {
			wb.write(new FileOutputStream("e:/a.cvs"));
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	//����
	public String imp(){
		try {
			Workbook wb = new HSSFWorkbook(new FileInputStream("e:/a.cvs"));
			Sheet st = wb.getSheetAt(0);
			for (int i = 1; i <= st.getLastRowNum(); i++) {
				Row row = st.getRow(i);
				Contacts con = new Contacts();
				System.out.println(row.getLastCellNum());
				String[] str = new String[row.getLastCellNum()];
				for (int j = 0; j < row.getLastCellNum(); j++) {
					Cell cell = row.getCell(j);
					if(cell != null) {
						str[j] = cell.getStringCellValue();
					}
				}
				con.setName(str[0]);
				con.setCompanyname(str[1]);
				con.setWeibo(str[2]);
				con.setWeibotype(str[3]);
				con.setContent(str[4]);
				con.setView(str[5]);
				con.setAddress(str[6]);
				con.setCreatetime(str[7]);
				Map<String,Object> map = ActionContext.getContext().getSession();
				User user = (User)map.get("user");
				con.setUser(user);
				con.setEnable(true);
				contactsService.save(con);
				String[] str2 = str[8].split(",");

				Set<Tel> telSet = new HashSet<Tel>();
				for (int j = 0; j < str2.length; j++) {
					Tel tel = new Tel();
					tel.setTel(str2[j]);
					tel.setCreatetime(TimeUtil.getTime());
					Contacts cs = contactsService.findA(con.getId());
					tel.setContacts(cs);
					telService.save(tel);
					telSet.add(tel);
				}
				String[] str3 = str[9].split(",");
				Set<Im> imSet = new HashSet<Im>();
				for (int j = 0; j < str3.length; j++) {
					Im im = new Im();
					im.setIm(str3[j]);
					im.setCreatetime(TimeUtil.getTime());
					Contacts cs = contactsService.findA(con.getId());
					im.setContacts(cs);
					imService.save(im);
					imSet.add(im);
				}
				String[] str4 = str[10].split(",");
				Set<Mail> mailSet = new HashSet<Mail>();
				for (int j = 0; j < str4.length; j++) {
					Mail mail = new Mail();
					mail.setMail(str4[j]);
					mail.setCreatetime(TimeUtil.getTime());
					Contacts cs = contactsService.findA(con.getId());
					mail.setContacts(cs);
					mailService.save(mail);
					mailSet.add(mail);
				}
				String[] str5 = str[11].split(",");
				Set<Website> websiteSet = new HashSet<Website>();
				for (int j = 0; j < str5.length; j++) {
					Website website = new Website();
					website.setCreatetime(TimeUtil.getTime());
					website.setWebsite(str5[j]);
					Contacts cs = contactsService.findA(con.getId());
					website.setContacts(cs);
					websiteService.save(website);
					websiteSet.add(website);
				}
				Message message = new Message();
				message.setContent(con.getContent());
				if (con.getName()==null) {
					String title =con.getCompanyname()+","+"addcontactsnote.action?id="+con.getId();
					message.setTitle(title);
				} else {
					String title =con.getName()+" "+con.getCompanyname()+","+"addcontactsnote.action?id="+con.getId();
					message.setTitle(title);
				}
				message.setCreatetime(TimeUtil.getTime());
				String type = "联系人"+","+"5FBEEE";
				message.setType(type);
				message.setUser(user);
				message.setView("all");
				message.setCtdid(con.getId());
				message.setEnable(true);
				messageService.save(message);
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String example(){
		Workbook wb = new HSSFWorkbook();
		Sheet st = wb.createSheet("联系表");
		Row topRow = st.createRow(0);
		
		Cell c1 = topRow.createCell(0);
		c1.setCellValue("name");
		Cell c2 = topRow.createCell(1);
		c2.setCellValue("companyname");
		Cell c3 = topRow.createCell(2);
		c3.setCellValue("weibo");
		Cell c4 = topRow.createCell(3);
		c4.setCellValue("weibotype");
		Cell c5 = topRow.createCell(4);
		c5.setCellValue("content");
		Cell c6 = topRow.createCell(5);
		c6.setCellValue("view");
		Cell c7 = topRow.createCell(6);
		c7.setCellValue("address");
		Cell c8 = topRow.createCell(7);
		c8.setCellValue("createtime");
		
		Cell c10 = topRow.createCell(8);
		c10.setCellValue("tel");
		Cell c11 = topRow.createCell(9);
		c11.setCellValue("im");
		Cell c12 = topRow.createCell(10);
		c12.setCellValue("mail");
		Cell c13 = topRow.createCell(11);
		c13.setCellValue("website");
		try {
			wb.write(new FileOutputStream("e:/contacts.xls"));
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	
	
	//setter,getter
	
	public ContactsService getContactsService() {
		return contactsService;
	}
	
	public Contacts getC() {
		return c;
	}
	public void setC(Contacts c) {
		this.c = c;
	}
	@Autowired
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	public String getG() {
		return g;
	}
	public void setG(String g) {
		this.g = g;
	}
	public String getU() {
		return u;
	}
	public void setU(String u) {
		this.u = u;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	@Autowired
	public void setImService(ImService imService) {
		this.imService = imService;
	}
	@Autowired
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}
	@Autowired
	public void setTelService(TelService telService) {
		this.telService = telService;
	}
	@Autowired
	public void setWebsiteService(WebsiteService websiteService) {
		this.websiteService = websiteService;
	}
	public List<Usergroup> getUglist() {
		return uglist;
	}
	public void setUglist(List<Usergroup> uglist) {
		this.uglist = uglist;
	}
	@Autowired
	public void setUsergroupService(UsergroupService usergroupService) {
		this.usergroupService = usergroupService;
	}
	public List<User> getUlist() {
		return ulist;
	}
	public void setUlist(List<User> ulist) {
		this.ulist = ulist;
	}
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public List<Contacts> getClist() {
		return clist;
	}
	public void setClist(List<Contacts> clist) {
		this.clist = clist;
	}
	@Autowired
	public void setContactsService(ContactsService contactsService) {
		this.contactsService = contactsService;
	}
	public Contacts getContacts() {
		return contacts;
	}
	public void setContacts(Contacts contacts) {
		this.contacts = contacts;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Pager<Contacts> getPager() {
		return pager;
	}
	public void setPager(Pager<Contacts> pager) {
		this.pager = pager;
	}
	public int getP() {
		return p;
	}
	public void setP(int p) {
		this.p = p;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Usergroup getGroup() {
		return group;
	}
	public void setGroup(Usergroup group) {
		this.group = group;
	}
	public String getCompanyname() {
		return companyname;
	}
	public void setCompanyname(String companyname) {
		this.companyname = companyname;
	}
	public String getWeibo() {
		return weibo;
	}
	public void setWeibo(String weibo) {
		this.weibo = weibo;
	}
	public String getWeibotype() {
		return weibotype;
	}
	public void setWeibotype(String weibotype) {
		this.weibotype = weibotype;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getView() {
		return view;
	}
	public void setView(String view) {
		this.view = view;
	}
	
	
}
